package com.arpa.wly.waybill;


import cn.hutool.json.JSONUtil;
import com.arpa.wms.domain.entity.Delivery;
import com.qimen.api.request.DeliveryorderConfirmRequest;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.CainiaoCloudprintMystdtemplatesGetRequest;
import com.taobao.api.request.CainiaoCloudprintStdtemplatesGetRequest;
import com.taobao.api.request.CainiaoWaybillIiGetRequest;
import com.taobao.api.request.CainiaoWaybillIiSearchRequest;
import com.taobao.api.response.CainiaoCloudprintMystdtemplatesGetResponse;
import com.taobao.api.response.CainiaoCloudprintStdtemplatesGetResponse;
import com.taobao.api.response.CainiaoWaybillIiGetResponse;
import com.taobao.api.response.CainiaoWaybillIiSearchResponse;
import com.taobao.pac.sdk.cp.PacClient;
import com.taobao.pac.sdk.cp.SendSysParams;
import com.taobao.pac.sdk.cp.dataobject.request.TMS_WAYBILL_DISCARD.TmsWaybillDiscardRequest;
import com.taobao.pac.sdk.cp.dataobject.request.TMS_WAYBILL_UPDATE.Recipient;
import com.taobao.pac.sdk.cp.dataobject.request.TMS_WAYBILL_UPDATE.TmsWaybillUpdateRequest;
import com.taobao.pac.sdk.cp.dataobject.response.TMS_WAYBILL_DISCARD.TmsWaybillDiscardResponse;
import com.taobao.pac.sdk.cp.dataobject.response.TMS_WAYBILL_UPDATE.TmsWaybillUpdateResponse;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


class TbWayBill {

    /**
     * 测试环境请求url
     */


    private static final String DAILY_URL = "https://eco.taobao.com/router/rest";

    /**
     * 测试环境appKey
     */


    private static final String APP_KEY = "33085419";
    private static final String SECRET_KEY = "061b6acd284f6c0c37bea017f513277d";

    private static final String TOKEN = "6100c141b4573ab67530c51a3c0379556ec9ad6d5d16070852353430";


    /**
     *
     * @author liuxr
     * @date 2021/10/29 10:59
     */
    @Test
    public void search(){
        TaobaoClient client = new DefaultTaobaoClient(DAILY_URL, APP_KEY, SECRET_KEY);
        CainiaoWaybillIiSearchRequest req = new CainiaoWaybillIiSearchRequest();
        req.setCpCode("YUNDA");
        CainiaoWaybillIiSearchResponse rsp = null;
        try {
            rsp = client.execute(req, TOKEN);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
    }


    @Test
    public void stdtemplates(){
        TaobaoClient client = new DefaultTaobaoClient(DAILY_URL, APP_KEY, SECRET_KEY);
        CainiaoCloudprintStdtemplatesGetRequest req = new CainiaoCloudprintStdtemplatesGetRequest();
        CainiaoCloudprintStdtemplatesGetResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
    }


    @Test
    public void mystdtemplates(){
        TaobaoClient client = new DefaultTaobaoClient(DAILY_URL, APP_KEY, SECRET_KEY);
        CainiaoCloudprintMystdtemplatesGetRequest req = new CainiaoCloudprintMystdtemplatesGetRequest();
        CainiaoCloudprintMystdtemplatesGetResponse rsp = null;
        try {
            rsp = client.execute(req, TOKEN);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());
    }




    @Test
    public void tmsWaybillGet() {
        TaobaoClient client = new DefaultTaobaoClient("https://eco.taobao.com/router/rest", "33085419", "061b6acd284f6c0c37bea017f513277d");
        CainiaoWaybillIiGetRequest req = new CainiaoWaybillIiGetRequest();
        CainiaoWaybillIiGetRequest.WaybillCloudPrintApplyNewRequest obj1 = new CainiaoWaybillIiGetRequest.WaybillCloudPrintApplyNewRequest();
        obj1.setCpCode("YUNDA");
        CainiaoWaybillIiGetRequest.UserInfoDto sender = new CainiaoWaybillIiGetRequest.UserInfoDto();
        CainiaoWaybillIiGetRequest.AddressDto address = new CainiaoWaybillIiGetRequest.AddressDto();
        address.setCity("临沂市");
        address.setDetail("临西12路与聚财七路交汇处1");
        address.setDistrict("兰山区");
        address.setProvince("山东省");
        sender.setAddress(address);
        sender.setMobile("13966408428");
        sender.setName("爱旺宠物");
        obj1.setSender(sender);




        List<CainiaoWaybillIiGetRequest.TradeOrderInfoDto> trade_order_info_dtos = new ArrayList<CainiaoWaybillIiGetRequest.TradeOrderInfoDto>();

        CainiaoWaybillIiGetRequest.TradeOrderInfoDto tradeOrderInfoDto = new CainiaoWaybillIiGetRequest.TradeOrderInfoDto();
        trade_order_info_dtos.add(tradeOrderInfoDto);


        tradeOrderInfoDto.setObjectId("FH202110280000014");
        CainiaoWaybillIiGetRequest.OrderInfoDto orderInfoDto = new CainiaoWaybillIiGetRequest.OrderInfoDto();
        orderInfoDto.setOrderChannelsType("TB");
        List<String> order = new ArrayList<>();
        order.add("2063126658952535615");
        orderInfoDto.setTradeOrderList(order);
        tradeOrderInfoDto.setOrderInfo(orderInfoDto);


        CainiaoWaybillIiGetRequest.PackageInfoDto obj10 = new CainiaoWaybillIiGetRequest.PackageInfoDto();
        List<CainiaoWaybillIiGetRequest.Item> list12 = new ArrayList<CainiaoWaybillIiGetRequest.Item>();
        CainiaoWaybillIiGetRequest.Item obj13 = new CainiaoWaybillIiGetRequest.Item();
        list12.add(obj13);
        obj13.setCount(2L);
        obj13.setName("新太空砂6L球形豆腐猫砂-原味奶香");
        obj10.setItems(list12);
        tradeOrderInfoDto.setPackageInfo(obj10);



        CainiaoWaybillIiGetRequest.RecipientInfoDto obj15 = new CainiaoWaybillIiGetRequest.RecipientInfoDto();
        CainiaoWaybillIiGetRequest.AddressDto obj16 = new CainiaoWaybillIiGetRequest.AddressDto();
        obj16.setCity("宜昌市");
        obj16.setDetail("当阳市**街道华强化工集团楚瓷**楼谢家湾超市");
        obj16.setProvince("湖北省");
        obj16.setDistrict("当阳市");


        obj15.setAddress(obj16);

        obj15.setMobile("*******9899");
        obj15.setName("罗**");
        obj15.setOaid("1Bxx01kFSOjp7B55afXRT4QkwKaOSRkYcVeYKv1bb9gdccmwDV9t3PYiblZhLuuJ1QrLPfjC");
        tradeOrderInfoDto.setRecipient(obj15);


        tradeOrderInfoDto.setTemplateUrl("http://cloudprint.cainiao.com/template/standard/288658/38");
        tradeOrderInfoDto.setUserId(197674857L);
        obj1.setTradeOrderInfoDtos(trade_order_info_dtos);
        req.setParamWaybillCloudPrintApplyNewRequest(obj1);
        System.out.println(JSONUtil.toJsonStr(obj1));
        CainiaoWaybillIiGetResponse rsp = null;
        try {
            rsp = client.execute(req, "6100c141b4573ab67530c51a3c0379556ec9ad6d5d16070852353430");
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(rsp.getBody());

    }

    public void tmsWaybillUpdate() {
        PacClient client = new PacClient(APP_KEY, SECRET_KEY, DAILY_URL);
        SendSysParams params = new SendSysParams();
        params.setFromCode(TOKEN);

        //更新一下面单上的收件人手机号
        TmsWaybillUpdateRequest request = new TmsWaybillUpdateRequest();

        //配送公司编码
        request.setCpCode("EMS");
        //面单号
        request.setWaybillCode("1101469709602");

        Recipient recipient = new Recipient();
        recipient.setMobile("13100000000");
        request.setRecipient(recipient);
        TmsWaybillUpdateResponse tmsWaybillUpdateResponse = client.send(request, params);
        if (!tmsWaybillUpdateResponse.isSuccess()) {
            return;
        }
        tmsWaybillUpdateResponse.getPrintData();
    }

    public void tmsWaybillDiscard() {
        PacClient client = new PacClient(APP_KEY, SECRET_KEY, DAILY_URL);
        SendSysParams params = new SendSysParams();
        params.setFromCode(TOKEN);

        TmsWaybillDiscardRequest request = new TmsWaybillDiscardRequest();
        //配送公司编码
        request.setCpCode("EMS");
        request.setWaybillCode("1101469709602");

        TmsWaybillDiscardResponse tmsWaybillDiscardResponse = client.send(request, params);
        if (!tmsWaybillDiscardResponse.isSuccess()) {
            return;
        }
    }

    private void deliveryConfirm(Delivery delivery) {
        DeliveryorderConfirmRequest req = new DeliveryorderConfirmRequest();
        req.setCustomerId("stub-cust-code");
        req.setVersion("2.0");
        DeliveryorderConfirmRequest.DeliveryOrder deliveryOrder = new DeliveryorderConfirmRequest.DeliveryOrder();
        // 出库单号
        deliveryOrder.setDeliveryOrderCode(delivery.getDeliveryOrderCode());
        // 仓库编码
        deliveryOrder.setWarehouseCode(delivery.getWarehouseCode());
        // 出库单类型
        deliveryOrder.setOrderType(delivery.getOrderType());
        // 包裹信息
        List<DeliveryorderConfirmRequest.Package> packages = new ArrayList<>();
        DeliveryorderConfirmRequest.Package package1 = new DeliveryorderConfirmRequest.Package();
        // 物流公司编码
        package1.setLogisticsCode(delivery.getLogisticsCode());
        // 运单号
        package1.setExpressCode(delivery.getWaybillCode());
        packages.add(package1);

        deliveryOrder.setPackages(packages);

        // 商品列表
        List<DeliveryorderConfirmRequest.Item> items = new ArrayList<>();
        DeliveryorderConfirmRequest.Item item = new DeliveryorderConfirmRequest.Item();
        // 商品编码
        item.setItemCode("1234");
        // 包裹内该商品数量
        item.setQuantity(BigDecimal.TEN.longValue());
        items.add(item);
        deliveryOrder.setItems(items);

        // 单据列表
        List<DeliveryorderConfirmRequest.OrderLine> orderLines = new ArrayList<>();
        DeliveryorderConfirmRequest.OrderLine orderLine = new DeliveryorderConfirmRequest.OrderLine();
        // 商品编码
        orderLine.setItemCode("1234");
        // 应发商品数量
        orderLine.setPlanQty("12");
        // 实发商品数量
        orderLine.setActualQty("12");
        orderLines.add(orderLine);

        req.setOrderLines(orderLines);

        // 创建json对象
        net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(req);

        net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(json.toString());

    }
}

